package com.example.a2032.net.jstylenet;


import android.text.TextUtils;

import com.amap.api.fence.GeoFence;
import com.amap.api.location.AMapLocation;
import com.example.a2032.Utils.DateUtils;
import com.example.a2032.Utils.SharedPreferenceUtils;
import com.example.a2032.entity.PPGMeasureData;
import com.example.a2032.entity.RealTimeData;
import com.example.a2032.entity.SleepData;
import com.example.a2032.entity.SmsInfo;
import com.example.a2032.entity.SnWarnData;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class NetSendUtils {
    //注册设备
    public static String registerDevice(String imei, String address, String phone) {
        NetBaseData<RegisterData> netBaseData = new NetBaseData<>();
        netBaseData.setType(JstyleConst.TYPE_REGISTER);
        netBaseData.setUser("");
        RegisterData registerData = new RegisterData();
        registerData.setImei(imei);
        registerData.setMac(address);
        registerData.setPhone(phone);
        netBaseData.setData(registerData);
        return ob2Json(netBaseData);
    }

    /**
     * 请求连接
     *
     * @param user
     * @return
     */
    public static String connect(String user) {

        NetBaseData netBaseData = new NetBaseData<>();
        netBaseData.setType(JstyleConst.TYPE_CONNECT);
        netBaseData.setUser(user);
        netBaseData.setData(new String());
        return ob2Json(netBaseData);
    }

    public static String getIm() {
        String user = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.watchId);
        if (TextUtils.isEmpty(user)) return "";
        NetBaseData netBaseData = new NetBaseData<>();
        netBaseData.setType(JstyleConst.TYPE_IM);
        netBaseData.setUser(user);
        netBaseData.setData("");
        return ob2Json(netBaseData);
    }

    public static String heartPackage(String lng, String lat, int battery) {
        String user = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.watchId);
        // user="123444444446777";
        NetBaseData<HeartPackageData> netBaseData = new NetBaseData<>();
        netBaseData.setType(JstyleConst.TYPE_PING);
        netBaseData.setUser(user);
        HeartPackageData heartPackageData = new HeartPackageData();
        heartPackageData.setLat(lat);
        heartPackageData.setLng(lng);
        heartPackageData.setBl(battery);
        netBaseData.setData(heartPackageData);

        return ob2Json(netBaseData);
    }

    public static String sosInfo(String lng, String lat) {
        String user = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.watchId);
        // user="123444444446777";
        NetBaseData<SosData> netBaseData = new NetBaseData<>();
        netBaseData.setType(JstyleConst.TYPE_Sos);
        netBaseData.setUser(user);
        SosData sosData = new SosData();
        sosData.setTime(DateUtils.getDefaultDate(new Date()));
        sosData.setLat(lat);
        sosData.setLng(lng);
        netBaseData.setData(sosData);
        return ob2Json(netBaseData);
    }

    public static String rtl(String lng, String lat, int battery, String from) {
        String user = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.watchId);
        // user="123444444446777";
        ExtendSendData<HeartPackageData> netBaseData = new ExtendSendData<>();
        netBaseData.setType(JstyleConst.TYPE_RTL);
        netBaseData.setUser(user);
        HeartPackageData heartPackageData = new HeartPackageData();
        heartPackageData.setLat(lat);
        heartPackageData.setLng(lng);
        heartPackageData.setBl(battery);
        netBaseData.setData(heartPackageData);
        ExtendSendData.ExtendBean extendBean = new ExtendSendData.ExtendBean();
        extendBean.setFrom(from);
        netBaseData.setExtend(extendBean);
        return ob2Json(netBaseData);
    }

    /**
     * 实时心率数据
     *
     * @param
     * @param hr
     * @return
     */
    public static String realTimeHrData(int hr, int state, String from) {
        String user = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.watchId);
        if (TextUtils.isEmpty(user) || (TextUtils.isEmpty(from) && state != JstyleConst.Measure_Finish))
            return "";
        RealTimeHeartRateData serviceRealTimeData = new RealTimeHeartRateData();
        serviceRealTimeData.setHr(hr);
        serviceRealTimeData.setState(state);
        ExtendSendData<ServiceRealTimeData> netBaseData = new ExtendSendData<>();
        netBaseData.setType(JstyleConst.TYPE_RTHR);
        netBaseData.setUser(user);
        netBaseData.setData(serviceRealTimeData);
        ExtendSendData.ExtendBean extendBean = new ExtendSendData.ExtendBean();
        extendBean.setFrom(from);
        netBaseData.setExtend(extendBean);
        return ob2Json(netBaseData);
    }

    public static String realTimeEcgData(List<Integer> data, int state, int heart, int hrv, String from) {
        String user = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.watchId);
        if (TextUtils.isEmpty(user)) return "";
        RealTimeEcgData realTimeEcgData = new RealTimeEcgData();
        realTimeEcgData.setState(state);
        realTimeEcgData.setEcg(data);
        realTimeEcgData.setHr(heart);
        realTimeEcgData.setHrv(hrv);
        ExtendSendData<RealTimeEcgData> netBaseData = new ExtendSendData<>();
        netBaseData.setType(JstyleConst.TYPE_RTECG);
        netBaseData.setUser(user);
        netBaseData.setData(realTimeEcgData);
        ExtendSendData.ExtendBean extendBean = new ExtendSendData.ExtendBean();
        extendBean.setFrom(from);
        netBaseData.setExtend(extendBean);
        return ob2Json(netBaseData);
    }


    /**
     * 通讯录白名单状态更改
     *
     * @param state
     * @return
     */
    public static String updateWhiteListState(String state) {
        String user = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.watchId);
        if (TextUtils.isEmpty(user)) return "";
        WhitelistData whitelistData = new WhitelistData();
        whitelistData.setState(state);
        ExtendSendData netBaseData = new ExtendSendData<>();
        netBaseData.setType(JstyleConst.TYPE_UpdateWl);
        netBaseData.setUser(user);
        netBaseData.setData(whitelistData);
        ExtendSendData.ExtendBean extendBean = new ExtendSendData.ExtendBean();
        extendBean.setFrom("");
        netBaseData.setExtend(extendBean);
        return ob2Json(netBaseData);
    }

    /**
     * 手表更改闹钟开关状态后发送消息给服务器
     *
     * @param id
     * @param state
     * @return
     */
    public static String updateClockState(String id, String state) {
        String user = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.watchId);
        if (TextUtils.isEmpty(user)) return "";
        UpdateClockStateData updateClockStateData = new UpdateClockStateData();
        updateClockStateData.setState(state);
        updateClockStateData.setId(id);
        ExtendSendData netBaseData = new ExtendSendData<>();
        netBaseData.setType(JstyleConst.TYPE_UpdateClock);
        netBaseData.setUser(user);
        netBaseData.setData(updateClockStateData);
        ExtendSendData.ExtendBean extendBean = new ExtendSendData.ExtendBean();
        extendBean.setFrom("");
        netBaseData.setExtend(extendBean);
        return ob2Json(netBaseData);
    }

    public static String snWarn(GeoFence geoFence) {
        AMapLocation aMapLocation = geoFence.getCurrentLocation();
        String lat = String.valueOf(aMapLocation.getLatitude());
        String lng = String.valueOf(aMapLocation.getLongitude());
        int type = geoFence.getStatus();
        long seq = Long.valueOf(geoFence.getCustomId());
        String user = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.watchId);
        if (TextUtils.isEmpty(user)) return "";
        SnWarnData snWarnData = new SnWarnData();
        snWarnData.setLat(lat);
        snWarnData.setLng(lng);
        snWarnData.setSeq(seq);
        snWarnData.setWarn(type);
        ExtendSendData netBaseData = new ExtendSendData<>();
        netBaseData.setType(JstyleConst.TYPE_SNWarn);
        netBaseData.setUser(user);
        netBaseData.setData(snWarnData);
        ExtendSendData.ExtendBean extendBean = new ExtendSendData.ExtendBean();
        extendBean.setFrom("");
        netBaseData.setExtend(extendBean);
        return ob2Json(netBaseData);
    }

    /**
     * 设置 成功/失败 后给服务器发送信息
     *
     * @param type
     * @param id
     * @param result
     * @param from
     * @return
     */
    public static String resultResponse(String type, String id, String result, String from) {
        String user = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.watchId);
        if (TextUtils.isEmpty(user)) return "";
        ResultData resultData = new ResultData();
        resultData.setResult(result);
        resultData.setId(id);
        ExtendSendData netBaseData = new ExtendSendData<>();
        netBaseData.setType(type);
        netBaseData.setUser(user);
        netBaseData.setData(resultData);
        ExtendSendData.ExtendBean extendBean = new ExtendSendData.ExtendBean();
        extendBean.setFrom(from);
        netBaseData.setExtend(extendBean);
        return ob2Json(netBaseData);
    }

    /**
     * 设置 成功/失败 后给服务器发送信息
     *
     * @param type
     * @param result
     * @param from
     * @return
     */
    public static String resultResponse(String type, String result, String from) {
        String user = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.watchId);
        if (TextUtils.isEmpty(user)) return "";
        ResultData resultData = new ResultData();
        resultData.setResult(result);
        ExtendSendData netBaseData = new ExtendSendData<>();
        netBaseData.setType(type);
        netBaseData.setUser(user);
        netBaseData.setData(resultData);
        ExtendSendData.ExtendBean extendBean = new ExtendSendData.ExtendBean();
        extendBean.setFrom(from);
        netBaseData.setExtend(extendBean);
        return ob2Json(netBaseData);
    }

    public static String resultResponseWithId(String type, List<String> idList, String from) {
        String user = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.watchId);
        if (TextUtils.isEmpty(user)) return "";

        ExtendSendData netBaseData = new ExtendSendData<>();
        netBaseData.setType(type);
        netBaseData.setUser(user);
        netBaseData.setData(idList);
        ExtendSendData.ExtendBean extendBean = new ExtendSendData.ExtendBean();
        extendBean.setFrom(from);
        netBaseData.setExtend(extendBean);
        return ob2Json(netBaseData);
    }

    /**
     * 实时血氧
     *
     * @param spo2
     * @return
     */
    public static String realTimeSpo2Data(int spo2, int state, String from) {
        String user = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.watchId);
        if (TextUtils.isEmpty(user) || TextUtils.isEmpty(from)) return "";
        ExtendSendData<RealTimeSpo2Data> netBaseData = new ExtendSendData<>();
        netBaseData.setType(JstyleConst.TYPE_RTSPO2);
        netBaseData.setUser(user);
        RealTimeSpo2Data serviceRealTimeData = new RealTimeSpo2Data();
        serviceRealTimeData.setSpo2h(spo2);
        serviceRealTimeData.setState(state);
        netBaseData.setData(serviceRealTimeData);
        ExtendSendData.ExtendBean extendBean = new ExtendSendData.ExtendBean();
        extendBean.setFrom(from);
        netBaseData.setExtend(extendBean);
        return ob2Json(netBaseData);
    }

    /**
     * 实时血压
     *
     * @param
     * @param sbp
     * @param dbp
     * @return
     */
    public static String realTimeBpData(int sbp, int dbp, int hrValue, int state, String from) {
        String user = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.watchId);
        if (TextUtils.isEmpty(user) || TextUtils.isEmpty(from)) return "";
        ExtendSendData<ServiceRealTimeData> netBaseData = new ExtendSendData<>();
        netBaseData.setType(JstyleConst.TYPE_RTBP);
        netBaseData.setUser(user);
        ServiceRealTimeData serviceRealTimeData = new ServiceRealTimeData();
        serviceRealTimeData.setSbp(sbp);
        serviceRealTimeData.setDbp(dbp);
        serviceRealTimeData.setHr(hrValue);
        serviceRealTimeData.setState(state);
        netBaseData.setData(serviceRealTimeData);
        ExtendSendData.ExtendBean extendBean = new ExtendSendData.ExtendBean();
        extendBean.setFrom(from);
        netBaseData.setExtend(extendBean);
        return ob2Json(netBaseData);
    }

    /**
     * 上传历史数据
     *
     * @param dataType
     * @param
     * @param dataTotalBeans
     * @return
     */
    public static String uploadHistoryData(String dataType, List dataTotalBeans) {
        String user = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.watchId);
        if (TextUtils.isEmpty(user) || dataTotalBeans.size() == 0) return "";
        ExtendSendData<UploadData> netBaseData = new ExtendSendData<>();
        netBaseData.setType(JstyleConst.TYPE_DATA);
        netBaseData.setUser(user);
        UploadData uploadData = new UploadData();
        uploadData.setDataType(dataType);
        uploadData.setDeviceType(JstyleConst.DataTYPE_Device);
        uploadData.setDataTotal(dataTotalBeans);
        netBaseData.setData(uploadData);
        ExtendSendData.ExtendBean extendBean = new ExtendSendData.ExtendBean();
        extendBean.setFrom("");
        netBaseData.setExtend(extendBean);
        return ob2Json(netBaseData);
    }

    /**
     * 上传心率历史数据
     *
     * @param
     * @param list
     * @return
     */
    public static String uploadHeartData(List<PPGMeasureData> list) {
        if (list.size() == 0) return "";
        List<UploadData.DataTotalBean> listHeartRateData = new ArrayList<>();
        for (PPGMeasureData heartRateData : list) {
            List<String> listHealth = new ArrayList<>();
            String date = heartRateData.getDate();
            int hrValue = heartRateData.getHeartRate();
            listHealth.add(hrValue + "");
            UploadData.DataTotalBean dataTotalBean = new UploadData.DataTotalBean();
            dataTotalBean.setTime(date);
            dataTotalBean.setDataDetail(listHealth);
            listHeartRateData.add(dataTotalBean);
        }
        return uploadHistoryData(JstyleConst.DataTYPE_StaticHr, listHeartRateData);
    }

    public static String upBPData(List<PPGMeasureData> list) {
        if (list.size() == 0) return "";
        List<UploadData.DataTotalBean> listHeartRateData = new ArrayList<>();
        for (PPGMeasureData heartRateData : list) {
            List<String> listHealth = new ArrayList<>();
            String date = heartRateData.getDate();
            int sbpValue = heartRateData.getSbpValue();
            int dbpValue = heartRateData.getDbpValue();
            listHealth.add(dbpValue + "");
            listHealth.add(sbpValue + "");
            UploadData.DataTotalBean dataTotalBean = new UploadData.DataTotalBean();
            dataTotalBean.setTime(date);
            dataTotalBean.setDataDetail(listHealth);
            listHeartRateData.add(dataTotalBean);
        }
        return uploadHistoryData(JstyleConst.DataTYPE_BP, listHeartRateData);
    }

    public static String upLoadSleepData(List<SleepData> list) {
        if (list.size() == 0) return "";
        List<UploadData.DataTotalBean> listHeartRateData = new ArrayList<>();
        for (SleepData sleepData : list) {
            List<String> listHealth = new ArrayList<>();
            String date = sleepData.getDate();
            String sleepQuality = sleepData.getSleepQuality();
            String[] sleepQualityArray = sleepQuality.split(" ");
            for (int i = 0; i < sleepQualityArray.length; i++) {
                listHealth.add(sleepQualityArray[i]);
            }
            UploadData.DataTotalBean dataTotalBean = new UploadData.DataTotalBean();
            dataTotalBean.setTime(date);
            dataTotalBean.setDataDetail(listHealth);
            listHeartRateData.add(dataTotalBean);
        }
        return uploadHistoryData(JstyleConst.DataTYPE_SleepData, listHeartRateData);
    }

    public static String upLoadStepData(List<RealTimeData> list) {
        if (list.size() == 0) return "";
        List<UploadData.DataTotalBean> listHeartRateData = new ArrayList<>();
        for (RealTimeData sleepData : list) {
            List<String> listHealth = new ArrayList<>();
            String date = sleepData.getDate();
            int steps = sleepData.getSteps();
            float distance = sleepData.getDistance();
            float cal = sleepData.getCal();
            listHealth.add(steps + "");
            listHealth.add(distance + "");
            listHealth.add(cal + "");
            UploadData.DataTotalBean dataTotalBean = new UploadData.DataTotalBean();
            dataTotalBean.setTime(date);
            dataTotalBean.setDataDetail(listHealth);
            listHeartRateData.add(dataTotalBean);
        }
        return uploadHistoryData(JstyleConst.DataTYPE_TotalData, listHeartRateData);
    }
//    public static String upEcgData(String mac, List<PPGMeasureData> list) {
//        if(list.size()==0)return "";
//        List<UploadData.DataTotalEcg> listHeartRateData = new ArrayList<>();
//        for (PPGMeasureData heartRateData : list) {
//            List<String> listHealth = new ArrayList<>();
//            String date = heartRateData.getDate();
//            int sbpValue = heartRateData.getSbpValue();
//            int dbpValue = heartRateData.getDbpValue();
//            listHealth.add(dbpValue + "");
//            listHealth.add(sbpValue + "");
//            UploadData.DataTotalEcg dataTotalBean = new UploadData.DataTotalEcg();
//            dataTotalBean.setTime(date);
//            dataTotalBean.setEcgs(listHealth);
//            listHeartRateData.add(dataTotalBean);
//        }
//        return uploadHistoryData(JstyleConst.DataTYPE_ECG, mac, listHeartRateData);
//    }

    /**
     * 血氧历史数据
     *
     * @param
     * @param list
     * @return
     */
    public static String upSpo2Data(List<PPGMeasureData> list) {
        if (list.size() == 0) return "";
        List<UploadData.DataTotalBean> listHeartRateData = new ArrayList<>();
        for (PPGMeasureData heartRateData : list) {
            List<String> listHealth = new ArrayList<>();
            String date = heartRateData.getDate();
            int hrValue = heartRateData.getMeasureValue();
            listHealth.add(hrValue + "");
            UploadData.DataTotalBean dataTotalBean = new UploadData.DataTotalBean();
            dataTotalBean.setTime(date);
            dataTotalBean.setDataDetail(listHealth);
            listHeartRateData.add(dataTotalBean);
        }
        return uploadHistoryData(JstyleConst.DataTYPE_Spo2, listHeartRateData);
    }

    public static String uploadCallLogs(List<UploadCallLogs> list) {
        String user = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.watchId);
        if (TextUtils.isEmpty(user)) return "";
        ExtendSendData<UploadCallLogs> netBaseData = new ExtendSendData<>();
        netBaseData.setType(JstyleConst.TYPE_Callr);
        netBaseData.setUser(user);
        netBaseData.setData(list);
        ExtendSendData.ExtendBean extendBean = new ExtendSendData.ExtendBean();
        extendBean.setFrom("");
        netBaseData.setExtend(extendBean);
        return ob2Json(netBaseData);
    }

    public static String uploadSmsInfos(List<SmsInfo> list) {
        String user = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.watchId);
        if (TextUtils.isEmpty(user)) return "";
        ExtendSendData<UploadCallLogs> netBaseData = new ExtendSendData<>();
        netBaseData.setType(JstyleConst.TYPE_Sms);
        netBaseData.setUser(user);
        netBaseData.setData(list);
        ExtendSendData.ExtendBean extendBean = new ExtendSendData.ExtendBean();
        extendBean.setFrom("");
        netBaseData.setExtend(extendBean);
        return ob2Json(netBaseData);
    }

    public static String ob2Json(NetBaseData netBaseData) {
        return new Gson().toJson(netBaseData);
    }
}
